package com.fastloan.mgt.product.approve.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fastloan.core.kuaixin.baidu.BaiDuModel;
import com.fastloan.mgt.expert.model.ExpertProduct;
import com.fastloan.mgt.expert.service.ExpertProductService;
import com.fastloan.mgt.log.aspect.SystemLog;
import com.fastloan.mgt.product.approve.model.ProductApprove;
import com.fastloan.mgt.product.approve.service.ProductApproveService;
import com.fastloan.safe.core.util.ManagerKit;
import com.fastloan.safe.mgt.org.service.CmuOrgService;
import com.fastloan.safe.mgt.user.service.CmuUserService;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenkai
 * createTime 2017-10-2017/10/20 16:10
 */
@RestController
@RequestMapping("/api/web/productApprove")
public class ProductApproveController extends BaseController<ProductApprove> {
    @Autowired
    private ProductApproveService productApproveService;

    @Autowired
    private CmuUserService cmuUserService;

    @Autowired
    private CmuOrgService cmuOrgService;

    @Autowired
    private ExpertProductService expertProductService;

    @GetMapping("/org")
    private ResultTo org() {
        return new ResultTo().setData(cmuOrgService.selectSonByTop(ManagerKit.getTopOrgId(), true, false));
    }

    /**
     * 机构下的用户
     *
     * @param orgId
     * @return
     */
    @GetMapping("/orgUser")
    private ResultTo orgUser(String orgId) {
        return new ResultTo().setData(cmuUserService.selectUserByOrgId(orgId, true));
    }

    /**
     * 产品分页
     *
     * @param productId
     * @return
     */
    @RequestMapping("/{productId}/pageList")
    public ResultTo pageList(@PathVariable String productId) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("productId", productId);
        return dataPage("nodePageList", map);
    }

    /**
     * 获取流程信息信息
     *
     * @param approveId
     * @return
     */
    @GetMapping("/{approveId}")
    public ResultTo get(@PathVariable String approveId) {
        return new ResultTo().setData(productApproveService.getNodeInfo(approveId));
    }

    /**
     * 获取当前产品流程信息
     *
     * @param productId
     * @return
     */
    @GetMapping("/nodeName/{productId}")
    public ResultTo getNodeNameList(@PathVariable String productId) {
        return productApproveService.getNodeNameList(productId);
    }

    /**
     * 新增流程信息信息
     *
     * @param productApprove
     * @return
     */
    @PostMapping
    @SystemLog(description = "新增流程信息信息")
    public ResultTo add(String expertProductList, ProductApprove productApprove) {
        List<ExpertProduct> list = JSON.parseArray(expertProductList, ExpertProduct.class);
        if (list != null && !list.isEmpty()) {
            expertProductService.insertList(list);
        }
        return productApproveService.add(productApprove) != 0 ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);
    }

    /**
     * 更新流程信息信息
     *
     * @param productApprove
     * @return
     */
    @PutMapping
    @SystemLog(description = "更新流程信息信息")
    public ResultTo update(String expertProductList, ProductApprove productApprove) {
        List<ExpertProduct> list = JSON.parseArray(expertProductList, ExpertProduct.class);
        if (list != null && !list.isEmpty()) {
            expertProductService.updateList(list);
        }
        return productApproveService.update(productApprove);
    }

    /**
     * 删除流程信息
     *
     * @param approveId
     * @return
     */
    @DeleteMapping("/{approveId}")
    public ResultTo delete(@PathVariable String approveId) {
        return new ResultTo().setData(productApproveService.deleteByPrimaryKey(approveId));
    }

    /**
     * 查询审批流程
     *
     * @param productId
     * @return
     */
    @GetMapping("/approveOrder/{productId}")
    public ResultTo approveOrder(@PathVariable String productId) {
        return productApproveService.approveOrder(productId);
    }

    /**
     * 保存审批流程
     *
     * @param approveIds
     * @return
     */
    @PostMapping("/changeApprove")
    @SystemLog(description = "保存审批流程")
    public ResultTo changeApprove(String approveIds) {
        try {
            return productApproveService.changeApprove(approveIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultTo(ResultEnum.OPERATION_FAILED, e.getMessage());
        }
    }

    @GetMapping("/test")
    public ResultTo test() {
        String blackScore = BaiDuModel.blackScore("孙立超", "210114198701251232", "15829551989");
        String multiScore = BaiDuModel.multiScore("孙立超", "210114198701251232", "15829551989");
        String risk = BaiDuModel.risk("孙立超", "210114198701251232", "15829551989");
        return new ResultTo().setData("blackScore", JSONObject.parse(blackScore))
                .setData("multiScore", JSONObject.parse(multiScore))
                .setData("risk", JSONObject.parse(risk));
    }
}
